package com.hzjh.base.style.view.loading;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class AnimationResourceModel implements Parcelable, Serializable {
    public static final int ONCE = 0;
    public static final int FOREVER = -1;
    public String dirName;   //动画名称
    public String id;     //动画资源ID
    public String prefixStr;
    public String extra;
    /**
     * 动画类型,包含有 origin,nature,frame,分别指代AE导出的用Lottie播放的动画资源，自定义动画，帧动画。
     */
    public String type;  //
    @Deprecated
    public boolean loop; //是否循环
    public int loopCount; //循环次数
    public int frameCount;
    /**
     * 播放完一个循环后是否停在当前帧
     */
    public boolean isStop;
    /**
     * 是否是可叠加动画
     */
    public boolean isAdd;

    public AnimationResourceModel(String dirName, String prefixStr, String type, boolean loop, int frameCount) {
        this.dirName = dirName;
        this.id = dirName;
        this.prefixStr = prefixStr;
        this.type = type;
        this.loop = loop;
        this.frameCount = frameCount;
        this.loopCount = loop ? Integer.MAX_VALUE : ONCE;
    }

    protected AnimationResourceModel(Parcel in) {
        dirName = in.readString();
        id = in.readString();
        prefixStr = in.readString();
        type = in.readString();
        loop = in.readByte() != 0;
        frameCount = in.readInt();
        loopCount = in.readInt();
        isStop = in.readByte() != 0;
    }

    public static final Creator<AnimationResourceModel> CREATOR = new Creator<AnimationResourceModel>() {
        @Override
        public AnimationResourceModel createFromParcel(Parcel in) {
            return new AnimationResourceModel(in);
        }

        @Override
        public AnimationResourceModel[] newArray(int size) {
            return new AnimationResourceModel[size];
        }
    };

    public AnimationResourceModel setFrameCount(int count) {
        this.frameCount = count;
        return this;
    }

    public AnimationResourceModel setLoopCount(int count) {
        this.loopCount = count;
        return this;
    }

    public AnimationResourceModel setStop(boolean stop) {
        isStop = stop;
        return this;
    }

    public AnimationResourceModel setCanAdd(boolean canAdd) {
        isAdd = canAdd;
        return this;
    }

    public AnimationResourceModel setExtra(String extra) {
        this.extra = extra;
        return this;
    }

    public String getExtra() {
        return extra;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(dirName);
        dest.writeString(id);
        dest.writeString(type);
        dest.writeByte((byte) (loop ? 1 : 0));
        dest.writeInt(frameCount);
        dest.writeInt(loopCount);
        dest.writeByte((byte) (isStop ? 1 : 0));
    }

    public AnimationResourceModel cloneMyself() {
        AnimationResourceModel model = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
            // stream to obj
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            model = (AnimationResourceModel) objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return model;
    }

}
